﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TriDVisualizer;
using EITSurferInfinityLib;

namespace ControlerLib
{
    /// <summary>
    /// Interaction logic for SlimDxControl.xaml
    /// </summary>
    public partial class SlimDxControl : UserControl
    {
        TriVisUserControl sdx;
        VirtualDevice device;

        public SlimDxControl(ref VirtualDevice mydevice)
        {
            InitializeComponent();
            device = mydevice;
            simpleGraph1.OnNewCurrentSelected += new SimpleGraph.NewCurrentSelectedEventHandler(simpleGraph1_OnNewCurrentSelected);
            simpleGraph1.OnNewReferenceSelected += new SimpleGraph.NewReferenceSelectedEventHandler(simpleGraph1_OnNewReferenceSelected);
        }

        void simpleGraph1_OnNewCurrentSelected(object sender, EventArgsSimpleGraph e)
        {
            device.StopReplay();
            device.SetCurrentFrame(e.index);
            device.GetSimpleDataIndex(e.index);
        }

        void simpleGraph1_OnNewReferenceSelected(object sender, EventArgsSimpleGraph e)
        {
            device.StopReplay();
            device.GetSimpleAndSetRef(e.index);
        }

   
       

        private void host_Loaded(object sender, RoutedEventArgs e)
        {
            sdx = new TriVisUserControl();
            host.Child = sdx;
        }

        private void buttonSetRef_Click(object sender, RoutedEventArgs e)
        {
            device.SetReference();
            simpleGraph1.SetReference();
        }

        private void buttonSetRec_Click(object sender, RoutedEventArgs e)
        {
            device.SetImageReconstruction("matlab library EIDORS\\Circ2_0_2_offset4");
        }

        public void Update(float[] dataIn, bool OnlyCursors, int index)
        {
            this.Dispatcher.Invoke((Action)(() =>
            {
                if (device.GetReplayMode()==PlayerStates.LiveStreaming)
                {
                    simpleGraph1.UpdateHistory(ref device.HistoryMean);
                    simpleGraph1.SetCurrent();
                }
                else
                {
                    simpleGraph1.SetCurrent(index); ;
                }
                simpleGraph1.Update();
                if (sdx != null)
                {
                    sdx.UpdateValue(dataIn);
                    sdx.Invalidate();
                }
                //this.UpdateLayout();
            }));
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            sdx.DisableAutoScale();
        }

        private void buttonResetThreshold_Click(object sender, RoutedEventArgs e)
        {
            sdx.ResetTheshold();
        }

        private void buttonPlusTheshold_Click(object sender, RoutedEventArgs e)
        {
            sdx.PlusTheshold();
        }

        private void buttonMinusThreshold_Click(object sender, RoutedEventArgs e)
        {
            sdx.MinusTheshold();
        }

        private void buttonHumanMesh_Click(object sender, RoutedEventArgs e)
        {
            string path = "InverseLibraries\\EIDORS\\Thoraxd2t3\\Thoraxd2t3_0_2_offset4";
            sdx.SetMesh(path);
            device.SetImageReconstruction(path);
        }

        private void buttonTankMesh_Click(object sender, RoutedEventArgs e)
        {
            string path = "InverseLibraries\\EIDORS\\Circd2c2\\Circd2c2_0_2_offset4";
            sdx.SetMesh(path);
            device.SetImageReconstruction(path);
            
        }

        private void buttonDoMovie_Click(object sender, RoutedEventArgs e)
        {
            sdx.StartMovieRecording("Thorax");
        }


        public void clearSimpleGraph()
        {
            simpleGraph1.data.Clear();
        }
        //public void setSimpleGraph()
        //{
            
        //    //if (device.HistoryMean.Count>0)
        //    //{
        //    //    simpleGraph1.data.Add(device.HistoryMean[device.HistoryMean.Count-1]);
        //    //    simpleGraph1.SetCurrent();
        //    //}

        //    simpleGraph1.SetCurrentPlusOne();

        //    this.Dispatcher.Invoke((Action)(() =>
        //    {
        //        simpleGraph1.Update();
        //    }));
            
        //}

        public void LoadHistory()
        {
            device.LoadFile();
            clearSimpleGraph();

            for (int i = 0; i < device.HistoryMean.Count; i++)
            {
                simpleGraph1.data.Add(device.HistoryMean[i]);
            }
            device.GetSimpleAndSetRef(0);
            this.Dispatcher.Invoke((Action)(() =>
            {
                simpleGraph1.Update();
            }));
            
        }
        private void simpleGraph1_Loaded(object sender, RoutedEventArgs e)
        {
            clearSimpleGraph();
        }

        private void buttonIconStartFile_Click(object sender, RoutedEventArgs e)
        {
            device.StartReplay();
        }

        private void buttonIconStopFile_Click(object sender, RoutedEventArgs e)
        {
            device.ResetReplay();
        }

        private void buttonIconResumeFile_Click(object sender, RoutedEventArgs e)
        {
            device.ResumeReplay();
        }

        public void SetCurrent(int index)
        {
            simpleGraph1.SetCurrent(index);
        }
        public void SetCurrentPlusOne()
        {
            simpleGraph1.SetCurrentPlusOne();
        }
    }
}
